import { useWindowStore } from '@/store/window'
import { useSysStore } from '@/store/sys'
import { IIconInfo } from "@/components/desktop-icon"

// import router from '@/router'

export const useOpenWin = (icon: IIconInfo) => {
  const windowStore = useWindowStore()
  const sysStore = useSysStore()

  // X方向剩余宽度
  let px = windowStore.w - sysStore.winSize.width
  // Y方向剩余高度
  let py = windowStore.h - sysStore.winSize.height
  //让随机的范围小一点，尽量居中
  let layerX = px > 0 ? Math.round(px / 4 + Math.random() * (px / 4)) : 0
  let layerY = py > 0 ? Math.round(py / 4 + Math.random() * (py / 4)) : 0

  //判断该winContent是否存在：是则该isShow属性展示，否则打开
  const winIndex = windowStore.winConfigs.findIndex((item) => item.wid === icon.id)
  windowStore.winIndex += 1
  if (winIndex === -1) {
    const wid = icon.id
    windowStore.winConfigs.push({
      name: icon.name,
      wid: wid,
      imgUrl: icon.imgUrl,
      isShow: true,
      zIndex: windowStore.winIndex,
      path: icon.path ?? '',
      pageUrl: icon.pageUrl,
      minImg: '',
      layerX,
      layerY
    })
  } else {
    windowStore.winConfigs[winIndex].isShow = true
    windowStore.winConfigs[winIndex].zIndex = windowStore.winIndex
  }
  windowStore.currentActiveId = icon.id

  // router.push({
  //   path: icon.path
  // })
}
